/** 
 * @Title: SlrankUtil.java 
 * @Package com.zizo.autohome.sta.slrank.util 
 * @Description: TODO
 * @author HeYan
 * @date 2016年10月22日 下午3:35:28 
 */
package com.zizo.autohome.sta.slrank.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zizo.util.ConnectionPool;

/**
 * @ClassName: SlrankUtil
 * @Description: TODO
 * @author HeYan
 * @date 2016年10月22日 下午3:35:28
 * 
 */
public class SlrankUtil {
	/**
	 * @Title: getCityLevel
	 * @Description: TODO 得到城市的级别
	 * @return
	 * @return Map<String,String>
	 * @author Alexis
	 */
	public final static Map<String, String> getCityLevel() {
		Map<String, String> map = new HashMap<>();
		String sql = "SELECT area_city,area_class,area_region FROM carvoice_core.core_area WHERE LENGTH(area_city)>0";
		Connection conn = ConnectionPool.getConnection("carvoice_core", "localhost");
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				String city = rs.getString(1);
				String level = rs.getString(2);
				String area = rs.getString(3);
				map.put(city, level + "-" + area);
			}
			map.put("", " - ");
			map.put("其它", " - ");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionPool.close(rs, st);
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return map;
	}

	/**
	 * @Title: getBBSTable
	 * @Description: TODO 计算所有的车型论坛表名称
	 * @return
	 * @return List<String>
	 * @author Alexis
	 */
	public static final List<String> getBBSTable() {
		List<String> list = new ArrayList<>();
		Statement st = null;
		ResultSet rs = null;
		Connection conn = ConnectionPool.getConnection("carvoice_biz", "localhost");
		String sql = "SELECT table_name AS tname from information_schema.`TABLES` WHERE TABLE_SCHEMA='autohome_syn' AND TABLE_NAME LIKE 'bbs_c_%'";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				list.add(rs.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionPool.close(rs, st);
		}
		return list;
	}

	/**
	 * @Title: getCarname
	 * @Description: TODO 得到给定的车型ID对应的车型名称
	 * @param carid
	 * @return
	 * @return String
	 * @author Alexis
	 */
	public final static Map<Integer, String> getCarname() {
		Map<Integer, String> map = new HashMap<>();
		Connection conn = ConnectionPool.getConnection("carvoice_biz", "localhost");
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT autohome_autohomeId,autohome_name from carvoice_core.core_brand_autohome";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				map.put(rs.getInt(1), rs.getString(2));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionPool.close(rs, st);
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return map;
	}

}
